<template>
  <el-dialog :title="title" :visible="showDialog" @close="closeEvent">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" status-icon label-width="100px" class="demo-ruleForm">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="ruleForm.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="ruleForm.code" type="password" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="ruleForm.managerId" placeholder="请选择">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="ruleForm.introduce" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="closeEvent">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addDepartment, getDepartment, getDepartmentjjj, getManagerList, updateDepartment } from '@/api/department'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentId: {
      type: Number,
      default: -1
    }

  },
  data() {
    return {

      managerList: [],
      ruleForm: {
        name: '',
        code: '',
        managerId: '',
        introduce: ''

      },
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称的长度为2-10个字符', trigger: 'blur' },
          {
            trigger: 'blur',
            // 自定义校验模式
            validator: async(rule, value, callback) => {
              let res = await getDepartment()

              if (this.ruleForm.id) {
              // 编辑场景
                res = res.filter(item => item.id !== this.ruleForm.id)
              }
              if (res.find(item => item.name === value)) {
                callback(new Error('部门名称已经存在'))
              } else {
                callback()
              }
            }
          }],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码的长度为2-10个字符', trigger: 'blur' },
          {
            trigger: 'blur',
            // 自定义校验模式
            validator: async(rule, value, callback) => {
              let res = await getDepartment()

              // 判断是否是编辑模式
              if (this.ruleForm.id) {
              // 编辑场景
                res = res.filter(item => item.id !== this.ruleForm.id)
              }
              if (res.find(item => item.code === value)) {
                callback(new Error('部门编码已经存在'))
              } else {
                callback()
              }
            }
          }
        ],
        managerId: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍的长度为1-100个字符', trigger: 'blur' }]
      }

    }
  },
  computed: {
    title() {
      return this.ruleForm.id ? '编辑' : '添加'
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    closeEvent() {
      this.ruleForm = {
        name: '',
        code: '',
        introduce: '',
        managerId: ''
      }
      this.$emit('update:showDialog', false)
      this.$refs.ruleForm.resetFields()
    },

    async getManagerList() {
      const res = await getManagerList()
      this.managerList = res
    },
    async submit() {
      await this.$refs.ruleForm.validate()
      if (this.ruleForm.id) {
        await updateDepartment(this.ruleForm)
      } else {
        await addDepartment({ ...this.ruleForm, pid: this.currentId })
      }

      this.$message.success('添加成功')
      this.$emit('updateDepartment')
      this.closeEvent()
    },
    async getDepartmentDetail() {
      this.ruleForm = await getDepartmentjjj(this.currentId)
    }
  }
}
</script>

<style scoped lang='less'></style>
